package com.studentmanage.manage_class.service;

import com.studentmanage.framework.domain.base.Clas;
import com.studentmanage.framework.domain.base.Learn;
import com.studentmanage.framework.domain.base.Specialize;
import com.studentmanage.framework.domain.base.response.ClassCode;
import com.studentmanage.framework.domain.common.response.CommonCode;
import com.studentmanage.framework.domain.common.response.QueryResult;
import com.studentmanage.framework.domain.common.response.ResponseResult;
import com.studentmanage.framework.domain.common.response.ResponseValue;
import com.studentmanage.manage_class.client.SpecializeClient;
import com.studentmanage.manage_class.dao.ClassMapper;
import com.studentmanage.manage_class.dao.ClassRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ClassService {
    @Autowired
    private ClassRepository classRepository;
    @Autowired
    private SpecializeClient specializeClient;
    @Autowired(required = false)
    private ClassMapper classMapper;

    //增加班级记录
    public ResponseResult addClass(Clas clas) {
        if (clas == null || StringUtils.isEmpty(clas.getSpecializeId())
                || StringUtils.isEmpty(clas.getClassCode())) {
            return new ResponseResult(ClassCode.ADD_FAIL);
        }
        Clas clas1 = classRepository.findByName(clas.getName());
        if (clas1 != null) {
            return new ResponseResult(ClassCode.CLASS_IS_EXIST);
        }
        ResponseValue<Specialize> responseValue = specializeClient.findSpecializeById(clas.getSpecializeId());
        if (!responseValue.isSuccess()) {
            return new ResponseResult(ClassCode.SPECIALIZE_IS_NOT_EXIST);
        }
        classRepository.save(clas);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //根据id查询班级信息
    public ResponseValue<Clas> findByCollegeId(String id) {
        Optional<Clas> optional = classRepository.findById(id);
        if (optional.isPresent()) {
            Clas clas = optional.get();
            return new ResponseValue<Clas>(true, clas);
        }
        return new ResponseValue<Clas>(false, null);
    }

    public ResponseResult updateClass(Clas clas) {
        if (clas == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        Optional<Clas> optional = classRepository.findById(clas.getId());
        if (!optional.isPresent()) {
            return new ResponseResult(CommonCode.FAIL);
        }
        classRepository.save(clas);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //查询班级选择列表
    public QueryResult<Clas> findListForStu() {
        List<Clas> all = classRepository.findAll();
        QueryResult<Clas> queryResult = new QueryResult<>();
        queryResult.setList(all);
        queryResult.setTotal(all.size());
        return queryResult;
    }

    //查询学籍信息
    public ResponseValue<Learn> findLearnByCollegeId(String id) {
        Learn learn = classMapper.findLearn(id);
        if (learn == null) {
            return new ResponseValue<Learn>(false, null);
        }
        return new ResponseValue<Learn>(true, learn);
    }
}
